package controllers

import (
	"go_server/models"
	"go_server/models/req"
	"go_server/service"
	"go_server/utils"
	"log"
	"net/http"
	"strconv"

	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson/primitive"
)

// 车辆档案相关接口处理
type VehicleInApi struct {
	VehicleService service.VehicleInformation
}

// 新增档案
func (v VehicleInApi) Add(c *gin.Context) {
	// 获取操作人
	var userName string
	claims, exists := c.Get("claims")
	if exists {
		userName = claims.(*utils.CustomClaims).UserName
	}
	var vehicle models.VehicleInformation
	if c.ShouldBind(&vehicle) == nil {
		if v.VehicleService.Add(&vehicle, userName) {
			c.JSON(http.StatusOK, gin.H{
				"code": 200,
				"msg":  "新增档案成功",
				"data": nil,
			})
		} else {
			c.JSON(http.StatusOK, gin.H{
				"code": 500,
				"msg":  "服务器错误",
				"data": nil,
			})
		}
	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"msg": "参数不正确",
		})
	}

}

// 查询档案列表
func (v VehicleInApi) FindList(c *gin.Context) {
	page, _ := strconv.ParseInt(c.Query("page"), 10, 64)
	pageSize, _ := strconv.ParseInt(c.Query("pageSize"), 10, 64)
	list, err := v.VehicleService.FindList(page, pageSize)
	if err == nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "操作成功",
			"data": list,
		})
	} else {
		log.Println(err.Error())
		c.JSON(http.StatusOK, gin.H{
			"code": -1,
			"msg":  "操作失败，服务器内部错误",
			"data": list,
		})
	}
}

// 查看详情
func (v VehicleInApi) FindDetails(c *gin.Context) {
	queryId := c.Query("id")
	id, err := primitive.ObjectIDFromHex(queryId)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"msg": "参数不正确",
		})
	} else {
		res := v.VehicleService.FindDetails(id)
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "操作成功",
			"data": res,
		})
	}
}

// 档案审核
func (v VehicleInApi) Audit(c *gin.Context) {
	body := new(req.AuditReq)
	err := c.ShouldBindJSON(body)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"msg": "参数不正确",
		})
	} else {
		// 查询档案是否存在
		res := v.VehicleService.FindDetails(body.Id)
		if res == nil {
			c.JSON(http.StatusOK, gin.H{
				"code": -1,
				"msg":  "没有该档案",
			})
		} else {
			b := v.VehicleService.Audit(body)
			if b {
				// 获取操作人
				var userName string
				claims, exists := c.Get("claims")
				if exists {
					userName = claims.(*utils.CustomClaims).UserName
				}
				v.VehicleService.UpdateTimeAndPeople(userName, body.Id)
				c.JSON(http.StatusOK, gin.H{
					"code": 200,
					"msg":  "审核成功",
				})
			} else {
				c.JSON(http.StatusInternalServerError, gin.H{
					"code": 500,
					"msg":  "服务器内部错误",
				})
			}
		}
	}
}

// 批量审核
func (v VehicleInApi) BatchAudit(c *gin.Context) {
	body := new(req.BatchAuditReq)
	err := c.ShouldBindJSON(body)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"msg": "参数不正确",
		})
	} else {
		b := v.VehicleService.BatchAudit(body)
		if b {
			// 获取操作人
			var userName string
			claims, exists := c.Get("claims")
			if exists {
				userName = claims.(*utils.CustomClaims).UserName
			}
			for _, id := range body.Ids {
				v.VehicleService.UpdateTimeAndPeople(userName, id)
			}
			c.JSON(http.StatusOK, gin.H{
				"code": 200,
				"msg":  "审核成功",
			})
		} else {
			c.JSON(http.StatusInternalServerError, gin.H{
				"code": 500,
				"msg":  "服务器内部错误",
			})
		}
	}
}

// 条件查询
func (v VehicleInApi) ConditionalFind(c *gin.Context) {
	var query req.ConditionalFindReq
	err := c.ShouldBindQuery(&query)
	log.Printf("===%+v\n", &query)
	if err == nil {
		list, err := v.VehicleService.ConditionalFind(&query)
		if err == nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 200,
				"msg":  "操作成功",
				"data": list,
			})
		} else {
			log.Println(err.Error())
			c.JSON(http.StatusOK, gin.H{
				"code": -1,
				"msg":  "操作失败，服务器内部错误",
				"data": list,
			})
		}
	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"msg": "参数不正确",
		})
	}

}

// 档案修改
func (v VehicleInApi) Update(c *gin.Context) {

}

// 档案删除
func (v VehicleInApi) Del(c *gin.Context) {
	body := new(req.DelReq)
	err := c.ShouldBind(body)
	if err == nil {
		v.VehicleService.DelById(body.ID)
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "操作成功",
		})
	} else {
		c.JSON(http.StatusBadRequest, gin.H{
			"msg": "参数不正确",
		})
	}
}
